﻿/*******************************************************************************
Copyright (c) 2011-2013, PKStudio

http://pkstudio.org

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*******************************************************************************/

using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace PKEngine
{
	/// <summary>
	/// Base class of channel.
	/// </summary>
	public class PKChannel
	{
		#region Public Properties
		public byte[] Data { get; private set; }
		public Size ContentSize { get; private set; }
		#endregion

		#region Initialization
		public PKChannel(Size contentSize)
		{
			Debug.Assert(contentSize.Width > 0 && contentSize.Height > 0, "PKChannel: Invalid size.");

			this.ContentSize = contentSize;
			this.Data = new byte[contentSize.Width * contentSize.Height];
		}

		public PKChannel(string filePath)
		{
			Debug.Assert(!string.IsNullOrEmpty(filePath), "PKChannel: Argument cannot be null.");
			Debug.Assert(File.Exists(filePath), "PKChannel: Cannot find file.");

			using (Bitmap bitmap = new Bitmap(filePath))
			{
				this.ContentSize = bitmap.Size;

				int length = bitmap.Size.Width * bitmap.Size.Height;
				this.Data = new byte[length];

				BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
				{
					byte[] temp = new byte[length * 3];
					System.Runtime.InteropServices.Marshal.Copy(data.Scan0, temp, 0, length * 3);
					for (int i = 0; i < length; i++)
					{
						this.Data[i] = temp[i * 3];
					}
				}
				bitmap.UnlockBits(data);
			}
		}

		public PKChannel(string filePath, Size contentSize)
			: this(contentSize)
		{
			Debug.Assert(!string.IsNullOrEmpty(filePath), "PKChannel: Argument cannot be null.");
			Debug.Assert(File.Exists(filePath), "PKChannel: Cannot find file.");

			using (Bitmap bitmap = new Bitmap(filePath))
			{
				int length = this.ContentSize.Width * this.ContentSize.Height;
				byte[] temp = new byte[length * 3];

				if (!bitmap.Size.Equals(this.ContentSize))
				{
					using (Bitmap scaledBitmap = new Bitmap(bitmap, this.ContentSize))
					{
						BitmapData data = scaledBitmap.LockBits(new Rectangle(0, 0, scaledBitmap.Width, scaledBitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
						System.Runtime.InteropServices.Marshal.Copy(data.Scan0, temp, 0, length * 3);
						scaledBitmap.UnlockBits(data);
					}
				}
				else
				{
					BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
					System.Runtime.InteropServices.Marshal.Copy(data.Scan0, temp, 0, length * 3);
					bitmap.UnlockBits(data);
				}

				for (int i = 0; i < length; i++)
				{
					this.Data[i] = temp[i * 3];
				}
			}
		}
		#endregion

		#region Pixel Methods
		/// <summary>
		/// Gets data of a pixel.
		/// </summary>
		/// <param name="x">X</param>
		/// <param name="y">Y</param>
		/// <returns>Data</returns>
		public byte GetPixel(int x, int y)
		{
			Debug.Assert(x >= 0 && x < ContentSize.Width && y >= 0 && y < ContentSize.Height, "Invalid point.");

			return Data[y * ContentSize.Width + x];
		}

		/// <summary>
		/// Sets data for a pixel.
		/// </summary>
		/// <param name="x">X</param>
		/// <param name="y">Y</param>
		/// <param name="value">Data</param>
		public void SetPixel(int x, int y, byte value)
		{
			if (x < 0 || y < 0 || x >= ContentSize.Width || y >= ContentSize.Height)
			{
				return;
			}

			Data[y * ContentSize.Width + x] = value;
		}
		#endregion
	}
}
